package com.buildings.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.api.Constant;
import com.buildings.base.BaseActivity;
import com.buildings.bean.LoginBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.JsonBuilder;
import com.buildings.http.MyHttpClient;
import com.buildings.util.Base64Test;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import com.buildings.util.EncryptUtil;
import com.buildings.util.SPUtil;
import com.buildings.util.Utils;
import com.bumptech.glide.Glide;
import com.yuyh.library.imgsel.ImageLoader;
import com.yuyh.library.imgsel.ImgSelActivity;
import com.yuyh.library.imgsel.ImgSelConfig;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class UserActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_modify_password)
    Button btnModifyPassword;
    @BindView(R.id.btn_logout)
    Button btnLogout;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_name_top)
    TextView tvNameTop;

    private Dialog myDialog;


    private static final int REQUEST_CODE = 102;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);
        tvTitle.setText("个人中心");
//        myDialog = DialogUtils.showRefreshDialog(activity);
        String userId = (String) SPUtil.get("userId","");
        String userName = (String) SPUtil.get("userName","");
        tvName.setText(userName);
        if(!TextUtils.isEmpty(userName)){
            char first = userName.charAt(0);
            tvNameTop.setText(first+"");
        }
//        getUserInfo(userId);
    }



    @OnClick({R.id.btn_modify_password,R.id.btn_logout,R.id.iv_head,R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_modify_password:
                startActivity(new Intent(activity,ModifyPasswordActivity.class));
                break;
            case R.id.btn_logout:
                Utils.logout(activity);
                finish();
                break;
            case R.id.iv_head:
                onClickHead();
                break;
        }
    }


    private void getUserInfo(String userId) {


        MyHttpClient
                .newBuilder()
                .get()
                .url(Api.USER_INFO+"?userId="+userId)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean bean) {
                        super.onSuccess(bean);
//{"code":200,"data":{"success":true,"errorCode":null,"errorMessage":null,"retryTimes":0,"remainLockedTime":null,"userId":"ca3ca0d0d557424d94ec8009b6ca6d08","token":"eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiJhNGQ1MTkxZC01ZjY0LTQ5YjEtYTk4NS01NjM2YTA5ZDc1M2EiLCJpYXQiOjE1ODk1MzMxNDksInN1YiI6IjkzNWQ4MzNkZGU0MjRlMWJhYzdmZWVjM2M0MDZhMDI5IiwiZXhwIjoxNTg5NTMzNTQ5fQ.KGhav1E8SLuHXkb60RsbWj3xLMscIPrGmvDSmPjSLr4"},"message":null}
                        if(bean.getCode() == 200){

                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }
                });

    }




    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
            if(pathList != null && pathList.size() > 0){
                String path = pathList.get(0);
                Log.e("onActivityResult","图片地址 "+path);
                Glide.with(activity).load(path).into(ivHead);
            }
        }
    }


    /**
     * 头像
     */
    public void onClickHead() {

        ImgSelConfig config = new ImgSelConfig.Builder(this, loader)
                // 是否多选
                .multiSelect(false)
                .btnText("Confirm")
                // 确定按钮背景色
                //.btnBgColor(Color.parseColor(""))
                // 确定按钮文字颜色
                .btnTextColor(Color.WHITE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.parseColor("#5887F9"))
                .title("图片选择")
                .titleColor(Color.WHITE)
                .titleBgColor(Color.parseColor("#5887F9"))
                .allImagesText("All Images")
                .needCrop(true)
                .cropSize(1, 1, 200, 200)
                // 第一个是否显示相机
                .needCamera(true)
                // 最大选择图片数量
                .maxNum(9)
                .build();

        ImgSelActivity.startActivity(this, config, REQUEST_CODE);

    }

    private ImageLoader loader = new ImageLoader() {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    };




}
